package leetcode.jianzhiOffer;

/**
 * @description: 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中，返回 true ；否则，返回 false 。
 * <p>
 * 单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
 * @author: cyy
 **/
public class jz_20220706_12 {
    public static void main(String[] args) {
        char[][] bord = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
        String word = "CEDA";
        System.out.println("result:" + existNew(bord, word));
    }

    public static boolean exist(char[][] board, String word) {
        int length = board.length;
        int width = board[0].length;
        if (length == 1) {
            StringBuffer stringBuffer = new StringBuffer();
            char[] res = board[0];
            for (int i = 0; i < width; i++) {
                stringBuffer.append(res[i]);
            }
            if (stringBuffer.toString().indexOf(word) != -1) {
                return true;
            }
        }
        int wordlength = word.length();
        int start = 0;
        char flag = word.charAt(start);
        int i = 0, j = 0, tem = 0;
        char top = 0, left = 0, right = 0, bom = 0;
        while (i < length && j < width) {
            //x轴为主
            for (int x = j; x < length; ) {
                if (board[i][x] == flag) {
                    if (wordlength == 1 || start == wordlength) {
                        return true;
                    }
                    tem = j;
                    flag = word.charAt(start++);
                    j = x;
                    if (j + 1 < width) {
                        right = board[x][j + 1];
                    }
                    if (i + 1 < length) {
                        bom = board[i + 1][j];
                    }
                    if (flag == right) {
                        j++;
                    }
                    if (flag == bom) {
                        i++;
                        j = tem;
                        break;
                    }
                }
            }
            //y轴w为主

        }


        return true;
    }

    /*
    *
    *  主要思路是从图中一个未访问的顶点 V 开始，沿着一条路一直走到底，然后从这条路尽头的节点回退到上一个节点，
    *  再从另一条路开始走到底...，不断递归重复此过程，直到所有的顶点都遍历完成，它的特点是不撞南墙不回头，
    *  先走完一条路，再换一条路继续走。
    *
     */
    public static boolean existNew(char[][] board, String word) {
        char[] words = word.toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (dfs(board, words, i, j, 0)) return true;
            }
        }
        return false;
    }

    public static boolean dfs(char[][] board, char[] word, int i, int j, int k) {
        if (i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k]) return false;
        if (k == word.length - 1) return true;
        board[i][j] = '\0';
        boolean res =
                dfs(board, word, i + 1, j, k + 1) ||
                dfs(board, word, i - 1, j, k + 1) ||
                dfs(board, word, i, j + 1, k + 1) ||
                dfs(board, word, i, j - 1, k + 1);
        board[i][j] = word[k];
        return res;
    }

}
